package com.transpot.service.impl.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.transpot.entry.customer.Contact;
import com.transpot.entry.customer.Customer;
import com.transpot.entry.customer.CustomerContact;
import com.transpot.entry.order.Agreement;
import com.transpot.entry.order.AgreementCustomerVO;
import com.transpot.entry.order.Order;
import com.transpot.entry.order.OrderBase;
import com.transpot.mapper.customer.ContactMapper;
import com.transpot.mapper.customer.CustomerContactMapper;
import com.transpot.mapper.customer.CustomerMapper;
import com.transpot.mapper.order.AgreementMapper;
import com.transpot.mapper.order.OrderBaseMapper;
import com.transpot.mapper.order.OrderMapper;
import com.transpot.service.order.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 合同类service层实现类
 */
@Service
public class AgreementServiceImpl extends ServiceImpl<AgreementMapper, Agreement> implements AgreementService {
    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerContactMapper customerContactMapper;
    @Autowired
    private ContactMapper contactMapper;
    @Autowired
    private OrderBaseMapper orderBaseMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public AgreementCustomerVO getAgreementAndCustomer(Integer id) {
        //根据合同id查询合同
        Agreement agreement = agreementMapper.selectById(id);
        //创建返回结果对象
        AgreementCustomerVO agreementCustomerVO = new AgreementCustomerVO();
        //设置合同信息
        agreementCustomerVO.setTitle(agreement.getTitle());
        agreementCustomerVO.setSecondParty(agreement.getSecondParty());
        agreementCustomerVO.setSecondPhone(agreement.getSecondPhone());
        //创建客户条件查询对象
        LambdaQueryWrapper<Customer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为与乙方姓名相同
        customerLambdaQueryWrapper.eq(Customer::getName, agreement.getSecondParty());
        //执行查询
        List<Customer> customerList = customerMapper.selectList(customerLambdaQueryWrapper);
        //如果只能查询到一个客户
        if (customerList.size() == 1) {
            //获取到该客户
            Customer customer = customerList.get(0);
            //设置客户地址
            agreementCustomerVO.setAddress(customer.getAddress());
            //设置客户编号
            agreementCustomerVO.setCarNum(customer.getCarNum());
            //设置客户姓名
            agreementCustomerVO.setName(customer.getName());
            //创建客户和联系方式中间类条件查询对象
            LambdaQueryWrapper<CustomerContact> customerContactLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //设置查询条件为与客户主键相同
            customerContactLambdaQueryWrapper.eq(CustomerContact::getCustomerId, customer.getId());
            //执行查询
            List<CustomerContact> customerContactList = customerContactMapper.selectList(customerContactLambdaQueryWrapper);
            //创建联系方式列表
            List<Contact> contactList = new ArrayList<>();
            //遍历所有的中间类对象
            for (CustomerContact customerContact : customerContactList) {
                //获取到联系方式主键
                Integer contactId = customerContact.getContactId();
                //执行查询获取到联系方式
                Contact contact = contactMapper.selectById(contactId);
                //把联系方式存到列表中
                contactList.add(contact);
            }
            agreementCustomerVO.setContactData(contactList);
            //当查询到的客户信息不唯一时
        } else if (customerList.size() > 1) {
            for (int i = 0; i < customerList.size(); i ++) {
                //获取到客户信息
                Customer customer = customerList.get(i);
                //创建客户和联系方式中间类条件查询对象
                LambdaQueryWrapper<CustomerContact> customerContactLambdaQueryWrapper = new LambdaQueryWrapper<>();
                //设置查询条件为与客户主键相同
                customerContactLambdaQueryWrapper.eq(CustomerContact::getCustomerId, customer.getId());
                //执行查询
                List<CustomerContact> customerContactList = customerContactMapper.selectList(customerContactLambdaQueryWrapper);
                //创建联系方式列表
                List<Contact> contactList = new ArrayList<>();
                //遍历所有的中间类对象
                for (CustomerContact customerContact : customerContactList) {
                    //获取到联系方式主键
                    Integer contactId = customerContact.getContactId();
                    //执行查询获取到联系方式
                    Contact contact = contactMapper.selectById(contactId);
                    //把联系方式存到列表中
                    contactList.add(contact);
                }
                //遍历获取到的联系方式列表
                for (int j = 0; j < contactList.size(); j ++) {
                    //获取到联系方式列表
                    Contact contact = contactList.get(j);
                    //如果联系方式与乙方相同
                    if (contact.equals(agreement.getSecondPhone())) {
                        //设置联系方式
                        agreementCustomerVO.setContactData(contactList);
                        //设置姓名
                        agreementCustomerVO.setName(customer.getName());
                        //设置地址
                        agreementCustomerVO.setAddress(customer.getAddress());
                        //设置客户id
                        agreementCustomerVO.setCarNum(customer.getCarNum());
                    }
                }
            }
        }
        return agreementCustomerVO;
    }

    /**
     * 根据id删除合同信息的方法实现
     * @param id
     * @return
     */
    @Override
    public boolean deleteAgreementById(Integer id) {
        //创建订单基本信息条件查询对象
        LambdaQueryWrapper<OrderBase> orderBaseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为合同订单主体与客户id相同
        orderBaseLambdaQueryWrapper.eq(OrderBase::getShipperContractSubject, id);
        //执行查询，获得订单基本信息列表
        List<OrderBase> orderBaseList = orderBaseMapper.selectList(orderBaseLambdaQueryWrapper);
        //如果根据合同id查不到订单信息，则可以删除合同
        if (orderBaseList.size() <= 0) {
            //根据合同id删除合同
            int i = agreementMapper.deleteById(id);
            if (i > 0) {
                return true;
            }
        } else {
            for (OrderBase orderBase : orderBaseList) {
                //获取订单基本信息主键
                Integer orderId = orderBase.getId();
                //设置订单整合类的条件查询对象
                LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
                //查询条件为基础信息主键相同
                orderLambdaQueryWrapper.eq(Order::getBaseId, orderId);
                //查询订单整合对象
                Order order = orderMapper.selectOne(orderLambdaQueryWrapper);
                if (order != null) {
                    //如果该订单已经完成
                    if (order.getStatus() == 3) {
                        //根据合同id删除合同
                        int i = agreementMapper.deleteById(id);
                        if (i > 0) {
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }
        return false;
    }
}
